import React from "react";
import {GlobalAction} from "./globalAction";

export const initialState = {
    demo:"这是一个全局数据",
    state:"user",
    productList:[]
};

export const GlobalStateContext = React.createContext(initialState);
export const GlobalDispatchContext= React.createContext(()=>{});


export function reducer(prevState, action) {

    switch (action.type) {
        case GlobalAction.ADD:
            return (()=>{
                if (!action.payload){
                    action.payload={}
                }
                return {...prevState,...action.payload};
            })()
        case GlobalAction.SET_ROLE:
            return (()=>{
                if (!action.payload){
                    action.payload={}
                }
                return {...prevState,...action.payload};
            })()
        case GlobalAction.SET_PRODUCT_LIST:
            return (()=>{
                if (!action.payload){
                    action.payload={}
                }
                return {...prevState,...action.payload};
            })()
        default:
            throw new Error("unknown type!");
    }
}

